#include <stdio.h>
#include <stdlib.h>

void swap(int* a, int* b);
void CountSort(int* arr, int n);
void Print(int* arr, int size);

void swap(int* a, int* b)
{
	int mid = *a;
	*a = *b;
	*b = mid;
}


void CountSort(int* arr, int n)
{
	int max = arr[0], min = arr[0];
	for (int i = 0; i < n; i++)
	{
		if (arr[i] > max)
			max = arr[i];
		if (arr[i] < min)
			min = arr[i];
	}
	int range = max - min + 1;
	int* tep = (int*)calloc(range, sizeof(int));
	for (int i = 0; i < n; i++)
	{
		tep[arr[i] - min]++;
	}
	int index = 0;
	for (int i = 0; i < range; i++)
	{
		while(tep[i])
		{
			arr[index++] = i + min;
			tep[i]--;
		}
	}
	free(tep);
	tep = NULL;
}

void Print(int* arr, int size)
{
	for (int i = 0; i < size; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int arr[] = { 1,0,3,6,2,5,8,7,4,9 };
	Print(arr, 10);
	CountSort(arr, 10);
	Print(arr, 10);
	return 0;
}